import 'package:flutter/cupertino.dart';

class MyTrianglePainter extends CustomPainter {
  final Color color;
  final int max;
  final int index;
  final double width;

  MyTrianglePainter({this.color, this.index, this.max, this.width});

  @override
  void paint(Canvas canvas, Size size) {
    Paint paint = Paint()
      ..color = color
      ..style = PaintingStyle.fill;
    canvas.drawPath(path(size.width, size.height), paint);
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) {
    return false;
  }

  Path path(double x, double y) {
    var w;
    if (max > 0) {
      w = x / max;
    }
    return Path()
      ..moveTo((w * index) + (w / 2) - width / 2, y)
      ..lineTo((w * index) + (w / 2), 0)
      ..lineTo((w * index) + (w / 2) + width / 2, y)
      ..close();
  }
}
